package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.SetMeal;
import com.itheima.reggie.mapper.CategoryMapper;

import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import net.sf.jsqlparser.statement.insert.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    public SetMealService setMealService;

    /**
     * 删除分类方法
     *
     * @param id
     * @return
     */
    @Override
    public int remove(Long id) {
        LambdaQueryWrapper<Dish> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,id);
        List<Dish> list = dishService.list(lqw);
        if (list!=null&&list.size()>0){
            throw  new CustomException("该分类中存在菜品，禁止删除");
        }
        LambdaQueryWrapper<SetMeal> setlqw =new LambdaQueryWrapper<>();
        setlqw.eq(SetMeal::getCategoryId,id);
        List<SetMeal> setMealList = setMealService.list(setlqw);
        if (setMealList!=null&&setMealList.size()>0){
            throw new CustomException("该套餐中存在菜品，禁止删除");
        }
        int row = baseMapper.deleteById(id);

        return row;
    }
}
